package com.sgb.kjwl.viewmodel.manage;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.RegexUtils;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.service.WebsiteInfoAPI;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.manage.CompanyTradesListEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author 张驰
 * @date 2021/5/17 21:55
 * @Description 经营动态model
 * @name com.sgb.kjwl.viewmodel.manage
 */
public class WebsiteUpDataJYDTModel extends BaseViewModel {

    //经营动态详情
    private MutableLiveData<CompanyTradesListEntity> companyTradesListEntityData = new MutableLiveData<>(new CompanyTradesListEntity());
    //动态类型
    private MutableLiveData<List<PickSelectEntity>> dynamiPickSelectList = new MutableLiveData<>(new ArrayList<>());
    //行业分类
    private MutableLiveData<List<PickSelectEntity>> typePickSelectList = new MutableLiveData<>(new ArrayList<>());

    private MutableLiveData<String> urlString = new MutableLiveData<>("");

    public MutableLiveData<String> getUrlString() {
        return urlString;
    }

    public MutableLiveData<CompanyTradesListEntity> getMutableLiveData() {
        return companyTradesListEntityData;
    }

    public MutableLiveData<List<PickSelectEntity>> getDynamiPickSelectList() {
        return dynamiPickSelectList;
    }

    public MutableLiveData<List<PickSelectEntity>> getTypePickSelectList() {
        return typePickSelectList;
    }

    /**
     * 经营动态-详情
     *
     * @param id
     */
    public void getCompanyNewsAdminDetailById(Context mActivity, String id) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getCompanyNewsAdminDetailById(WebsiteInfoAPI.NetBuilderUrl.COMPANY_NEWS_ADMIN_DETAIL_BY_ID,id).enqueue(new ICallBack<BaseEntity<CompanyTradesListEntity>>() {
            @Override
            public void onSuccess(BaseEntity<CompanyTradesListEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    companyTradesListEntityData.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 编辑或新增
     */
    public void saveCompanyNewsAdminOrUpdate(Context mActivity, Map<String, Object> map) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().saveCompanyNewsAdminOrUpdate(WebsiteInfoAPI.NetBuilderUrl.SAVE_OR_UPDATE,map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    getMessage().postValue("操作成功");
                }
            }
        });
    }

    /**
     * 行业分类
     */
    public void getAllCategory(Context mActivity) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getAllCategory().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    typePickSelectList.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 动态类型
     */
    public void getDynamicList(Context mActivity) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getDynamicList(WebsiteInfoAPI.NetBuilderUrl.DYNAMIC_LIST).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    dynamiPickSelectList.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 图片上传
     * @param mContext
     * @param file
     */
    public void uploadAvatar(Context mContext, File file) {
        DialogHelper.showProgressDialog(mContext, null, "图片上传中...", 0, false, null).setCanceledOnTouchOutside(true);
        //创建map集合
        Map<String, RequestBody> photos = new HashMap<>();
        //将文件转换成多媒体文件，方便上传
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + RegexUtils.getReplaceAll(file.getName(), "[\u4e00-\u9fa5]", "") + "image.jpg\"", requestBody);
        //接口请求
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data == null || data.getData() == null) {
                    MToast.showToast(mContext, "上传失败");
                    return;
                }
                urlString.postValue(data.getData().toString());
            }
        });
    }

    /**
     *  根据路由获取H5链接访问
     * @param mContext
     * @param body 传入的参数
     * @param param 路由地址
     * @param code 返回的Code码
     */
    public void getH5Url(Context mContext, String body, String param, int code,String title) {
        DialogHelper.showProgressDialog(mContext, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?title=" + title;
                    WebViewX5HostActivity.start(mContext, url,body, code);
                } else {
                    MToast.showToast(mContext, data.getMsg());
                }
            }
        });
    }

}
